package com.wsapi.core.dao.model;

import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.codehaus.jackson.annotate.JsonIgnore;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

@Entity
@Table(name = "sys_role")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class SysRole {

    private Integer roleId;
    private String roleName;
    private String roleExplain;

    private Set<SysAccount> accounts;
    private Set<SysRolePerm> permissions;

    @Id
    @Column(name = "role_id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public Integer getRoleId() {
        return roleId;
    }

    public void setRoleId(Integer roleId) {
        this.roleId = roleId;
    }

    @Column(name = "role_name")
    public String getRoleName() {
        return roleName;
    }

    public void setRoleName(String roleName) {
        this.roleName = roleName;
    }

    @Column(name = "role_explain")
    public String getRoleExplain() {
        return roleExplain;
    }

    public void setRoleExplain(String roleExplain) {
        this.roleExplain = roleExplain;
    }

    @ManyToMany
    @JoinTable(name = "SYS_ACC_ROLE", joinColumns = {
        @JoinColumn(name = "role_id")}, inverseJoinColumns = {
        @JoinColumn(name = "acc_id")})
    @JsonIgnore
    public Set<SysAccount> getAccounts() {
        return accounts;
    }

    public void setAccounts(Set<SysAccount> accounts) {
        this.accounts = accounts;
    }

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "role", fetch = FetchType.EAGER)
    @Fetch(FetchMode.JOIN)
    public Set<SysRolePerm> getPermissions() {
        return permissions;
    }

    public void setPermissions(Set<SysRolePerm> permissions) {
        this.permissions = permissions;
    }

}
